package com.wd.buycar.view.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.wd.buycar.R
import com.wd.buycar.view.activity.OldCarDetailsActivity
import com.wd.common.bean.BuyCarBean

/**
 * 作者：马海钊
 * 时间：2023/9/11 17:23
 * 功能：
 */
class BuyoldcarAdapterList(var context: Context, var list: List<BuyCarBean.Buy_OldCar_clooselist.Result>) :
    RecyclerView.Adapter<BuyoldcarAdapterList.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view =
            LayoutInflater.from(context).inflate(R.layout.buy_oldcar_list_small, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.name.text = list[position].carStyleName
        holder.price.text = list[position].sellingPrice.toString()+"W"
        //holder.proportion.text = list[position].upPlateNumberTime.toString()
        holder.collect.setOnCheckedChangeListener { button, b ->
            if (holder.collect.isChecked){
                ToastUtils.showLong("收藏成功")
            }
        }
        holder.itemView.setOnClickListener {
            val intent = Intent(context, OldCarDetailsActivity::class.java)
            intent.putExtra("id",list.get(position).carId)
            context.startActivity(intent)
        }
        Glide.with(context).load(list[position].bgImageUrl)
            .error(R.mipmap.buy_oldcar_back1)
            .placeholder(R.mipmap.buy_oldcar_back1)
            .into(holder.imgid)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var name: TextView
        var price: TextView
        var proportion: TextView
        var collect: CheckBox
        var imgid: ImageView

        init {
            name = itemView.findViewById(R.id.buy_oldcar_list_small_name)
            price = itemView.findViewById(R.id.buy_oldcar_list_small_price)
            proportion = itemView.findViewById(R.id.buy_oldcar_list_small_proportion)
            collect = itemView.findViewById(R.id.buy_oldcar_list_small_collect)
            imgid = itemView.findViewById(R.id.buy_oldcar_list_small_imgid)
        }
    }
}